/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.UserLogin_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class UserLogin_BLL {
    ConnectUnit connection;
    
    public UserLogin_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    /**
     * get list of UserLogin form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public HashSet<UserLogin_DTO> getUserLogins(String condition, String OrderBy) throws Exception{
        HashSet<UserLogin_DTO> userlogins = new HashSet<UserLogin_DTO>();
        ResultSet result = this.connection.Select("USERLOGIN", condition, OrderBy);
        while(result.next()){
            UserLogin_DTO user = new UserLogin_DTO();
            user.setUserID(result.getString("UserID"));
            user.setUserName(result.getString("UserName"));
            user.setUserPassword(result.getString("UserPassword"));
            user.setPermissionID(result.getString("PermissionID"));
            userlogins.add(user);
        }
        return userlogins;
    }
    
    /**
     * get list of UserLogin form database with Condition
     * @param condition
     * @return
     * @throws Exception 
     */
    public HashSet<UserLogin_DTO> getUserLogins(String condition) throws Exception{
        return this.getUserLogins(condition, null);
    }
    
    /**
     * get list of UserLogin form database
     * @return
     * @throws Exception 
     */
    public HashSet<UserLogin_DTO> getUserLogins() throws Exception{
        return this.getUserLogins(null, null);
    }
    
    /**
     * get UserLogin by UserID in database
     * @param ID
     * @return
     * @throws Exception 
     */
    public UserLogin_DTO getUserLogin_byUserID(String ID) throws Exception{
        HashSet<UserLogin_DTO> users = this.getUserLogins("UserID="+ ID);
        if(users.size()>0)
            return users.toArray(new UserLogin_DTO[users.size()])[0];
        return null;      
    }
    
    /**
     * Insert a UserLogin into database
     * @param info
     * @throws Exception 
     */
    public void Insert(UserLogin_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("UserID", info.getUserID());
        map.put("UserName", info.getUserName());
        map.put("UserPassword", info.getUserPassword());
        map.put("PermissionID", info.getPermissionID());
        
        String[] arr = {"UserID","UserName","UserPassword","PermissionID",};
        this.connection.Insert("USERLOGIN", map, arr);
    }
    
    /**
     * Update a UserLogin by UserID into database
     * @param info
     * @throws Exception 
     */
    public void Update(UserLogin_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("UserID", info.getUserID());
        map.put("UserName", info.getUserName());
        map.put("UserPassword", info.getUserPassword());
        map.put("PermissionID", info.getPermissionID());
        
        this.connection.Update("USERLOGIN", map, "UserID="+ info.getUserID());
    }
    
    /**
     * Delete a UserLogin by UserID in database
     * @param info
     * @throws Exception 
     */
    public void Delete(UserLogin_DTO info) throws Exception{
        this.connection.Delete("USERLOGIN", "UserID="+ info.getUserID());
    }
}
